package com.jqsoft.launcher.activity;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jqsoft.launcher.activity.childCareManagement.ChildCareManageActivity;
import com.jqsoft.launcher.activity.diabetesMellitus.DiabetesMellitusDiagnosisActivity;
import com.jqsoft.launcher.activity.gairui.OlderPersonManager;
import com.jqsoft.launcher.activity.gairui.XDpersonActivity;
import com.jqsoft.launcher.activity.gairui.YiliaoServiceActivity;
import com.jqsoft.launcher.activity.highblood.HBPDiagnosisActivity;
import com.jqsoft.launcher.activity.majorPsychosis.MajorPsychosisManageActivity;
import com.jqsoft.launcher.activity.pregnantWomen.PregnantWomenManageActivity;
import com.jqsoft.launcher.activity.setting.LiXianActivity;
import com.jqsoft.launcher.activity.setting.SettingActivity;
import com.jqsoft.launcher.activity.task.TaskListActivity;
import com.jqsoft.launcher.common.BphsConstants;
import com.jqsoft.launcher.R;
import com.jqsoft.launcher.print.PrintUtil;
import com.jqsoft.launcher.util.MessageTools;

/**
 * 主界面
 *
 * @author gujunqi date：2013-12-27
 */
// 如果无网络连接activeInfo为null

public class MainFunctionActivity extends JQActivity implements OnClickListener {
    private final String tag = "MainFunctionActivity";
    private GridView gridView;

    protected Button online, exit, parameter, message;

    // 图片数组
    private final int[] imagesId = {R.drawable.mp_btn_1_on,//
            R.drawable.mp_btn_2_on, //
            R.drawable.mp_btn_3_on,//
            R.drawable.mp_btn_4_on,//
            R.drawable.mp_btn_5_on
            , R.drawable.mp_btn_10_on
            , R.drawable.mp_btn_11_on, R.drawable.mp_btn_12_on
    };
    // 字符串数组
    private final int[] strItem = {R.string.menu_item_01,
            R.string.menu_item_02, R.string.menu_item_03,
            R.string.menu_item_04, R.string.menu_item_05, R.string.menu_item_10,
            R.string.menu_item_11, R.string.menu_item_12};//

    @Override
    protected int getContentLayout() {
        return R.layout.mainfunction_layout;
    }

    @Override
    protected void initGui() {
        // registerNetReceiver();
        gridView = (GridView) findViewById(R.id.function_gridView);
        GridViewAdapter adapter = new GridViewAdapter(act);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new Glistener());
        // 实例化按钮
        online = (Button) findViewById(R.id.online_mode);
        exit = (Button) findViewById(R.id.exit);
        parameter = (Button) findViewById(R.id.parameter);
        message = (Button) findViewById(R.id.message);
        // 按钮点击事件
        exit.setOnClickListener(this);
        parameter.setOnClickListener(this);
        message.setOnClickListener(this);
        online.setOnClickListener(this);
        // title_bar_right_button.setVisibility(View.VISIBLE);

    }

    @Override
    protected void initAction() {

    }

    @Override
    protected void initData() {
        setTitle("全科医生巡诊及随访系统");
        setGoBack_Gone();
        if (!BphsConstants.isNetAvailable) {
            online.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.offline));

        } else {
            online.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.online));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.exit:// 退出按钮
                exitDialog();
                break;
            case R.id.parameter:// 参数设置
                toNoFinish(SettingActivity.class);
                break;
            case R.id.message:// 消息提醒
                if (!BphsConstants.isNetAvailable) {
                    MessageTools.showDialogOk(ctx, "此功能暂不支持离线操作，请连线操作！");
                    return;
                }
                toNoFinish(TaskListActivity.class);
                break;
            case R.id.online_mode:
                if (!BphsConstants.isNetAvailable) {
                    toNoFinish(LiXianActivity.class);
                } else {
                    toNoFinish(LiXianActivity.class);
                }
                break;
            default:
                break;
        }
    }

    private void testPrint() {
        PrintUtil.printCustomDocument(this);

//        String url = "http://www.qq.com";
//        PrintUtil.printUrlByWebView(this, url);

//        String htmlDocument = "<html><body><table width=100% border=\"1px solid #F00\"><tr><th width=%30>姓名</th><th width=%70>内容</th></tr>";
//        for (int i = 0; i < 70; ++i){
//            String html = "<tr><td width=%30>陈旭"+i+"</td><td width=%70>内容"+i+"</td></tr>";
//            htmlDocument+=html;
//        }
//         htmlDocument +=  "</table></body></html>";
//        PrintUtil.printHtmlStringByWebView(this, htmlDocument);

//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//        PrintUtil.printBitmap(this, bitmap);
    }

    /**
     * 网格元素点击事件
     *
     * @作者：周桃有
     * @日期：2015-7-21
     */
    public class Glistener implements OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            switch (position) {
                case 0: //
//                    testPrint();

//                    // 高血压管理
//                    Intent intent = new Intent(MainFunctionActivity.this,
//                            HBPDiagnosisActivity.class);
//                    startActivity(intent);
                    toNoFinish(HBPDiagnosisActivity.class);
                    return;
                case 1:
                    // 糖尿病确诊管理
                    Intent intent2 = new Intent(MainFunctionActivity.this,
                            DiabetesMellitusDiagnosisActivity.class);
                    startActivity(intent2);
                    // toNoFinish(DiabetesMellitusDiagnosisActivity.class);
                    return;
                case 2:
                    // 重性精神病患者随访管理
                    Intent intent3 = new Intent(MainFunctionActivity.this,
                            MajorPsychosisManageActivity.class);
                    startActivity(intent3);
                   //  toNoFinish(MajorPsychosisManageActivity.class);
                    return;
                case 3:
                    // 儿童保健管理 ChildCareManageActivity
                    // if (!BphsConstants.isNetAvailable) {
                    // MessageTools.showDialogOk(ctx, "此功能暂不支持离线操作（正在开发中），请连线操作！");
                    // return;
                    // }
                    Intent intent4 = new Intent(MainFunctionActivity.this,
                            ChildCareManageActivity.class);
                    startActivity(intent4);
                    // toNoFinish(ChildCareManageActivity.class);
                    return;
                case 4:
                    // 孕产妇保健管理页面
                    // if (!BphsConstants.isNetAvailable) {
                    // MessageTools.showDialogOk(ctx, "此功能暂不支持离线操作（正在开发中），请连线操作！");
                    // return;
                    // }
                    // Intent intent5 = new Intent(MainFunctionActivity.this,
                    // PregnantWomenManageActivity.class);
                    // startActivity(intent5);
                    toNoFinish(PregnantWomenManageActivity.class);
                    return;
                // case 5:
                // // 人员信息列表
                // toNoFinish(LiXianActivity.class);
                // return;
                case 5: // 心电
                    // if (1 != BphsConstants.deviceBranchType) {
                    // MessageTools.showDialogOk(ctx, "此功能暂时只支持盖瑞设备！");
                    // } else {
                    toNoFinish(XDpersonActivity.class);
                    // / }
                    return;
                case 6: // 医疗服务
                    // if (1 != BphsConstants.deviceBranchType) {
                    // MessageTools.showDialogOk(ctx, "此功能暂时只支持盖瑞设备！");
                    // } else {
                    toNoFinish(YiliaoServiceActivity.class);
                    // }
                    return;
                case 7: // 老年人管理

                    toNoFinish(OlderPersonManager.class);
                    // }
                    return;
                default:
                    break;
            }
            Toast.makeText(act, "即将上线，敬请期待！", Toast.LENGTH_SHORT).show();
        }
    }

    public void exitDialog() {
        // 对话框
        Builder dialog = new Builder(this).setMessage("您确定要退出系统吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        exitSystem();
                    }
                }).setNegativeButton("取消", null);
        MessageTools.showDialog(dialog);
    }

    // ---------------------屏蔽 home,back键----------------------------
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(tag, "单击监听.keyCode:(3:home,4:back)" + keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK
                || keyCode == KeyEvent.KEYCODE_HOME) {
            Log.i(tag, "退出系统提示...");
            exitDialog();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    class GridViewAdapter extends BaseAdapter {

        public Context con;
        public LayoutInflater inflater;

        private GridViewAdapter(Context ctx) {
            con = ctx;
        }

        public int getCount() {
            return imagesId.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = null;
            inflater = LayoutInflater.from(con);
            if (convertView != null) {
                itemView = convertView;
            } else {// menu_toolbar_row
                itemView = inflater.inflate(R.layout.main_tool_row, null);
            }
            ImageView iv = (ImageView) itemView.findViewById(R.id.bar_image);
            iv.setImageResource(imagesId[position]);
            TextView tv = (TextView) itemView.findViewById(R.id.bar_title);
            tv.setText(strItem[position]);
            return itemView;
        }
    }
    // NetBroadcastReceiver netBroadcastReceiver ;
    // //监听网络状态
    // private void registerNetReceiver() {
    // IntentFilter filter = new
    // IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    // netBroadcastReceiver = new
    // NetBroadcastReceiver();//这应该是个全局变量，为了方便观看，才写成了局部
    // this.registerReceiver(netBroadcastReceiver, filter);
    // netBroadcastReceiver.setNetContentListener(new
    // NetBroadcastReceiver.NetContentListener() {
    // @SuppressWarnings("deprecation")
    // @Override
    // public void netContent(boolean isConnected) {
    // if (isConnected) {
    // // Toast.makeText(getApplicationContext(), "网络连接",
    // Toast.LENGTH_SHORT).show();
    // Log.i("das","网络连接" );
    // online.setBackgroundDrawable(getResources().getDrawable(
    // R.drawable.online));
    // } else {
    // Toast.makeText(getApplicationContext(), "网络已经断开",
    // Toast.LENGTH_SHORT).show();;
    // online.setBackgroundDrawable(getResources().getDrawable(
    // R.drawable.offline));
    //
    // }
    // }
    // });
    // }
    // @Override
    // public void onDestroy() {
    // // TODO Auto-generated method stub
    // super.onDestroy();
    // this.unregisterReceiver(netBroadcastReceiver);
    // }
}
